package com.lai.security;

import com.lai.dao.JdbcUserDao;
import com.lai.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * User: wlai
 * Date: 7/30/13
 * Time: 2:19 PM
 * To change this template use File | Settings | File Templates.
 */
@Component
public class MyAuthenticationProvider implements AuthenticationProvider {
    @Autowired
    private JdbcUserDao userDao;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        boolean isValid = userDao.validateLogin(authentication.getName(), authentication.getCredentials().toString());
        if (isValid) {
            User user = userDao.getUserByUsername(authentication.getName());
            return new UsernamePasswordAuthenticationToken(authentication.getName(),
                    authentication.getCredentials(), user.getAuthorities());

        }

        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public boolean supports(Class<?> aClass) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
